package com.zh.rent.controller;

import com.zh.rent.pojo.Account;
import com.zh.rent.pojo.Guide;
import com.zh.rent.pojo.Setup;
import com.zh.rent.pojo.StandbyAccount;
import com.zh.rent.service.AccountService;
import com.zh.rent.service.IGuideService;
import com.zh.rent.service.SetupService;
import com.zh.rent.service.StanAccountService;
import com.zh.rent.util.HttpContextUtils;
import com.zh.rent.util.JsonResult;
import com.zh.rent.util.SpringContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/guide")
@Api(tags="新手引导与反馈")
public class guideController {
     @Autowired
      IGuideService IGuideServiceImpl;
    @Autowired
    AccountService AccountServiceImpl;
    @Autowired
    StanAccountService StanAccountServiceImpl;

    @Autowired
    SetupService SetupServiceImpl;
    @GetMapping("/selimg")
    @ApiOperation(value = "图片显示")
    public JsonResult selimg() {
        try {
            List<Guide> guides = IGuideServiceImpl.selguiint(0);
            return JsonResult.OK(guides);
        }catch (Exception e){
            return JsonResult.ERROR("显示失败");
        }
    }
    @GetMapping("/selcall")
    @ApiOperation(value = "客服显示")
    public JsonResult selcall() {
        try {
            List<Guide> guides = IGuideServiceImpl.selguiint(2);
            return JsonResult.OK(guides);
        }catch (Exception e){
            return JsonResult.ERROR("显示失败");
        }
    }

    @PostMapping("/setfeedback")
    @ApiOperation(value = "反馈提交")
    public JsonResult setfeedback(String guidetext, String invCode,Integer Accountid) {
        try {
            String guidePath = "";
            Map<String, Object> map = new HashMap<>();
            map.put("guidetext", guidetext);
            map.put("guidePath", guidePath);
            map.put("pathType", 1);
            map.put("invCode", invCode);
            map.put("Accountid", Accountid);
            int i = IGuideServiceImpl.insetGuidefeedback(map);
            if (i == 0) return JsonResult.ERROR("提交失败！");
            List<Map<String, Object>> list = IGuideServiceImpl.selguiAccountid();
            Setup setup = SetupServiceImpl.setsetupAll();
            for (Map<String, Object> strMap : list) {
                int count = (Integer) ((Long) strMap.get("count")).intValue();
                System.out.println("ssss" + count);
                if (count >= setup.getFeedback()) {
                    Account account = AccountServiceImpl.selAcc((Integer) strMap.get("Accountid"));
                    StandbyAccount standbyAccount = StanAccountServiceImpl.selStanAccint(account.getAccountType());
                    int i1 = AccountServiceImpl.updateStan(standbyAccount.getStandbyid(), account.getAccountid());
                }
            }
            return JsonResult.OK("提交成功！");
        }catch (Exception e){
            return JsonResult.ERROR("发现错误："+e.getMessage());
        }
    }
}
